/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gcs.persistencia;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * @author Rafe
 */
public abstract class GestorBD {
    
    private static Connection conn =  null;
        
    public static void inicilizar() throws Exception
    {
        String url = "jdbc:mysql://localhost:3306/GCSoftcombat";
       
        
        Class.forName("com.mysql.jdbc.Driver");       
        conn = DriverManager.getConnection(url,"root","");

    }
    
    public static Connection getConnection() {
        
        return conn;
    }
    
    public static void ejecutarConsulta(String comando, Statement stmt, ResultSet rs)throws Exception{
        stmt = conn.createStatement();
        rs = stmt.executeQuery(comando);
    }
    
    public static void ejecutarActualizacion(String comando)throws Exception{
        
        Statement st = conn.createStatement();
        
        st.execute(comando);
        
        st.close();
    }
    
    public static Integer ejecutarActualizacionConAutoID(String comando)throws Exception{
        
        Statement st = conn.createStatement();
        
        st.execute(comando, Statement.RETURN_GENERATED_KEYS);
        
        ResultSet rs = st.getGeneratedKeys();
        
        Integer id = null;
        
        if(rs.next()){
            id=rs.getInt(1);
        }else{
            throw new Exception("ERROR: Problema amb l'autoincrement");
        }
        
        rs.close();
        
        st.close();
        
        return id;
    }
    
    public static void finalizar() throws Exception
    {
        conn.close();
    }
    
}
